# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Leo <thinkabit.ukim@gmail.com>
pkgname=glslang
pkgver=8.13.3743
pkgrel=0
pkgdesc="Khronos reference front-end for GLSL, ESSL, and sample SPIR-V generator"
url="https://github.com/KhronosGroup/glslang"
arch="all"
license="BSD-3-Clause"
depends_dev="$pkgname"
makedepends="cmake samurai python3 bison spirv-tools-dev"
checkdepends="bash"
subpackages="$pkgname-static $pkgname-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/KhronosGroup/glslang/archive/$pkgver.tar.gz
	pkgconfig.patch
	default-resources.patch
	"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	mkdir -p build-shared build-static
	(cd build-shared
		cmake \
			-GNinja \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DCMAKE_INSTALL_LIBDIR=lib \
			-DCMAKE_BUILD_TYPE=None \
			-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
			-DCMAKE_C_FLAGS="$CFLAGS" \
			-DBUILD_SHARED_LIBS=ON \
			$CMAKE_CROSSOPTS ..
		ninja
	)
	sed -i '/add_library(glslang-default-resource-limits/ s/$/ STATIC/' StandAlone/CMakeLists.txt
	(cd build-static
		cmake \
			-GNinja \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DCMAKE_INSTALL_LIBDIR=lib \
			-DCMAKE_BUILD_TYPE=None \
			-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
			-DCMAKE_C_FLAGS="$CFLAGS" \
			-DBUILD_SHARED_LIBS=OFF \
			$CMAKE_CROSSOPTS ..
		ninja
	)
}

check() {
	[ "$CARCH" = s390x ] && return 0
	cd Test
	msg "Testing shared version of glslang"
	./runtests localResults ../build-shared/StandAlone/glslangValidator ../build-shared/StandAlone/spirv-remap
	msg "Testing static version of glslang"
	./runtests localResults ../build-static/StandAlone/glslangValidator ../build-static/StandAlone/spirv-remap
}

package() {
	DESTDIR="$pkgdir" ninja -C build-shared install
	DESTDIR="$pkgdir" ninja -C build-static install
}

sha512sums="19169c4ffa511113978af8e8efc57b290d4aa962a4c2429dbea1af72204787c6ce20ffbbb452dc04bef5416e00510819d84463a2e76295b9a59b409e12af6792  glslang-8.13.3743.tar.gz
9490ed637a8f4a032fb0e0d266843b4ffccedce5983de1f16f96348c7b8340d4d8791f3a6a247f53ef5ea256deecf674d8cf0030d77b7d7d396091aed5bbb404  pkgconfig.patch
f526443c070fc3e4639cdec0a4876e5a75529af4cf41282ba23b0d9234434f764bf55b93aaaf7ed1de757193842f3d0b51437cf62c41d955dd86907fcc1a911e  default-resources.patch"
